/*
 * object-manager.jsp页面专用js文件
 * 前置：common/script/customize/customize.js
 * 主要是针对objectType功能使用的js
 */

var objectTypeDialog_searchText = "";

function initTypeDatagrid(){
    if($('#typeDatagrid').attr('init-flag') == 'true'){
        return;
    }
    initTypeDatagridToolBar();
    generateEditor();
    $('#typeDatagrid').datagrid({
        url:basePath+'objectAction/searchObjectType.json'
        ,method:'post'
        ,queryParams:{'text':''}
        ,idField:'objectId'
        ,border:false
        ,cache:false
        ,rownumbers:true
        ,toolbar:'#typeDatagridToolbar'
        ,fit:true
        ,striped:true
        //,fitColumns:true
        ,singleSelect:true
        ,columns:[[
            {field:'objectCode',title:'类别编码',width:100,editor:'text'}
            ,{field:'objectName',title:'类别名称',width:100,editor:'text'}
            ,{field:'operator',title:'操作',hidden:true,width:100,editor:'operator',formatter:function(value,row,index){
                return'----';
            }}
            ,{field:'stateText',title:'类别状态',width:100,formatter:function(value,row,index){
                if(row.state == 1){return '启用';}
                if(row.state == 0){return '禁用';}
            }}
        ]]
        ,onDblClickRow:function(index,row){

        }
        ,onBeginEdit:function(index, row){
            if(editIndex != index){
                $('#typeDatagrid').datagrid('cancelEdit',editIndex);
            }
            $('#typeDatagrid').datagrid('showColumn','operator');
            editIndex = index;
        }
        ,onCancelEdit:function (index,row) {
            if(row.objectId == -1){
                $('#typeDatagrid').datagrid('deleteRow',index);
                editIndex = -1;
            }
            $('#typeDatagrid').datagrid('hideColumn','operator');
            editIndex = -1;
        }
        ,onEndEdit:function (index, row, changes) {
            $('#typeDatagrid').datagrid('hideColumn','operator');
            editIndex = -1;
        }
        ,onAfterEdit:function (index, row, changes) {
            //对数据库保存修改的操作
            if(row.objectId == -1){
                //save
                if(!saveObjectType(changes.objectCode,changes.objectName)){
                    $('#typeDatagrid').datagrid('deleteRow',index);
                }
            }else{
                //update
                if($.isEmptyObject(changes)){return;}
                updateObjectType(row.objectId,changes);
            }
            $('#typeDatagrid').datagrid('load',{'text':objectTypeDialog_searchText});
        }
    });
    $('#typeDatagrid').attr('init-flag','true');
}
function updateObjectType(objectId,changes){
    var params = {'objectId':objectId};
    $.extend(params,changes);
    $.ajax({
        async:false
        ,url:basePath+'objectAction/updateType.json'
        ,type:'post'
        ,data:params
        ,dataType:'json'
        ,success:function (ajaxData) {
            if(getAjaxReturnData(ajaxData)!= null){
                $.messager.alert('成功',"更新成功",'info',emptyFn());
            }
        }
    });
}

function saveObjectType(objectCode,objectName){
    if(objectCode == null || objectCode == '' || objectCode == 'undefined'){
        alert('类别编码不能为空！');
        return false;
    }
    if(objectName == null || objectName == '' || objectName == 'undefined'){
        alert('类别名称不能为空！');
        return false;
    }
    var flag = true;
    $.ajax({
        async:false
        ,url:basePath+'objectAction/saveType.json'
        ,type:'post'
        ,data:{'objectCode':objectCode,'objectName':objectName}
        ,dataType:'json'
        ,success:function(ajaxData){
            if(getAjaxReturnData(ajaxData)!= null){
                $.messager.alert('成功',"添加成功",'info',emptyFn());
            }
        }
    });
    return flag;
}


function generateEditor(){
    var operator = {
        operator : {
            init:function(container, options){
                var okBtn = $('<a></a>');
                var cancelBtn= $('<a></a>');
                okBtn.appendTo(container);
                cancelBtn.appendTo(container);
                okBtn.linkbutton({
                    iconCls:'icon-ok'
                    ,onClick:function(){
                        $('#typeDatagrid').datagrid('endEdit',editIndex);
                    }
                });
                cancelBtn.linkbutton({
                    iconCls:'icon-cancel'
                    ,onClick:function () {
                        $('#typeDatagrid').datagrid('cancelEdit',editIndex);
                    }
                });

            },
            getValue: function(target){

            },
            setValue: function(target, value){

            },
            resize: function(target, width){
            }

        }
    };
    $.extend($.fn.datagrid.defaults.editors,operator);
}



function initTypeDatagridToolBar(){
    var toolBar = $('#typeDatagridToolbar');
    var searchBox = $('<input />');
    var addBtn = $('<a >添加</a>');
    var updBtn = $('<a >修改</a>');
    var stateChangeBtn = $('<a>禁用/启用</a>');
    toolBar.append(searchBox);
    toolBar.append(addBtn);
    toolBar.append(updBtn);
    toolBar.append(stateChangeBtn);
    toolBar.css('padding','5px');

    searchBox.textbox({
        buttonIcon:'icon-search'
        ,onClickButton:function(){
            objectTypeDialog_searchText = searchBox.textbox('getText');
            $('#typeDatagrid').datagrid('load',{'text':objectTypeDialog_searchText});
        }
    });

    var margin = {'margin':'0px 5px'};
    addBtn.css(margin);
    addBtn.linkbutton({
        iconCls:'icon-add'
        ,onClick:function () {
            var param = {index:0,row:{objectId:-1}};

            if(editIndex != -1){
                $('#typeDatagrid').datagrid('selectRow',editIndex)
                var row = $('#typeDatagrid').datagrid('getSelected');
                if(row.objectId == -1){return;}
                editIndex++;
            }
            $('#typeDatagrid').datagrid('insertRow',param);
            $('#typeDatagrid').datagrid('beginEdit',0);

        }
    });
    updBtn.css(margin);
    updBtn.linkbutton({
        iconCls:'icon-edit'
        ,onClick:function () {
            var row = $('#typeDatagrid').datagrid('getSelected');
            if(row != null){
                $('#typeDatagrid').datagrid('beginEdit',$('#typeDatagrid').datagrid('getRowIndex',row));
            }
        }
    });
    stateChangeBtn.css(margin);
    stateChangeBtn.linkbutton({
        iconCls:'icon-lock'
    });

}